/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost.OnTabChangeListener;

/**
 * 抽象的TabFragment
 * @author Chaos	
 * @date 2012-12-18 
 */
public abstract class AbsTabFragment extends BaseFragmentWrapper
                        implements OnTabChangeListener{

    private View       mAncestor;
    private TabHostICS mTabHost;

    @Override
    protected View onCreateViewPort(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        
        mAncestor = initLayout(inflater, container, savedInstanceState);
        
        if(mAncestor == null){
            throw new RuntimeException("Make sure you to layout is correct!");
        }
        
        View tabHost = mAncestor.findViewById(android.R.id.tabhost);
        
        if(tabHost == null || !(tabHost instanceof TabHostICS)){
            throw new RuntimeException("You must use the prescriptive TabHostICS class!");
        }
        
        mTabHost = (TabHostICS) tabHost;
        mTabHost.setup(this, getFragmentManager(), getContentViewId());
        mTabHost.setOnTabChangedListener(this);
        
        initTabs(mTabHost);
        return mAncestor;
    }
    
    @Override
    protected void dealloc() {
        mTabHost  = null;
        mAncestor = null;
        super.dealloc();
    }
    
    /**
     * 初始化一个包含TabHostICS的布局
     * @param 
     * @return 
     * @throws
     */
    protected abstract View initLayout(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState);
    
    /**
     * 不需要调用setup(),直接初始化Tab。
     */
    protected abstract void initTabs(TabHostICS tabHost);
    
    protected abstract int getContentViewId();

}
